package com.engine.modules.ytht.mapper;

import com.engine.modules.ytht.entity.FarmersFieldsEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author FY
 * @version 1.0.0
 * @ClassName FarmersBindingMapper.java
 * @Description TODO
 * @createTime 2021年12月16日
 */
@Repository
@Slf4j
public class FarmersBindingMapper {

    @Autowired
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    public String updateByFarmersFieldsEntity(String id_card,Long year,String field_id) {
        Map<String, Object> map = new HashMap<>();
        map.put("id_card", id_card);
        map.put("year", year);
        map.put("field_id", field_id);
        String sql="SELECT year FROM r_famers_fields WHERE id_card=:id_card AND field_id=:field_id AND year=:year";
        List<Map<String, Object>> year2= namedParameterJdbcTemplate.queryForList(sql,map);
        if(year2.size()>0)
            return field_id;
        sql = "INSERT INTO r_famers_fields(id,id_card,field_id,year,gmt_create,gmt_update,del_flag)" +
                "VALUES (uuid_generate_v4(),:id_card,:field_id,:year,NOW(),NOW(),'0')";
        namedParameterJdbcTemplate.update(sql, map);
        return field_id;
    }

    public int selectBundingCountByFieldId(String field_id)
    {
        Map<String,Object> map =new HashMap<>();
        map.put("field_id",field_id);
        String sql="SELECT field_id FROM r_famers_fields WHERE field_id=:field_id";
        return namedParameterJdbcTemplate.queryForList(sql,map).size();
    }


    public int deleteByFarmersFieldsEntity(String id_card,Long year,String field_id)
    {
        List<String> result=new ArrayList<>();
        Map<String,Object> map =new HashMap<>();
        map.put("id_card",id_card);
        map.put("year",year);
        map.put("field_id",field_id);
        String sql="DELETE FROM r_famers_fields WHERE id_card=:id_card AND field_id=:field_id AND year=:year";
        int x =namedParameterJdbcTemplate.update(sql, map);
        return x;
    }

    public FarmersFieldsEntity seleteFarmersFieldsEntityByIdCard(String id_card,Long year)
    {
        FarmersFieldsEntity farmersFieldsEntity=new FarmersFieldsEntity();
        Map<String,Object> map =new HashMap<>();
        map.put("id_card",id_card);
        map.put("year",year);
        String sql="SELECT id_card,year,field_id FROM r_famers_fields WHERE id_card=:id_card AND year=:year";
        List<Map<String,Object>> rs=namedParameterJdbcTemplate.queryForList(sql, map);
        if(rs.size()!=0)
        {
            farmersFieldsEntity.setIdCard(id_card);
            farmersFieldsEntity.setYear(year);
        }
        List<String> field_idList=new ArrayList<>();
        for (Map<String,Object> m: rs) {
            field_idList.add(m.get("field_id").toString());
        }
        farmersFieldsEntity.setFieldId(field_idList);
        return farmersFieldsEntity;
    }
}
